var Zone = {};

Zone.modify = {
    eventer : {},
    model : null,
    init : function(model) {
        var _this = this;

        this.model = model;

        $(this.eventer).on('getPlan', function(e, j) {
            _this.renderPlanList(j);
        })

        $(this.eventer).on('getAdByPlanId', function(e, j) {
            _this.renderAdList(j.plan_id, j.d);
        });

        $('#plan-list-container').on('click', '.plan-list-row__act', function(e) {
            var domId = $(this).parent().attr('id'),
                planId = (domId.split('-')).pop(),
                $adListContainer = $('#ad-list-container-' + planId);
                val = $(this).html();

            if (val == '展开') {
                $adListContainer.show();

                if (!$adListContainer[0]) {
                    _this.getAdByPlanId(planId);
                    _this.renderAdList(planId, {s : false , m : '正在载入广告数据，请稍等...'});
                }
                $(this).html('折叠');
            } else {
                $adListContainer.hide();
                $(this).html('展开');
            }
        })

        $('#plan-list-container').on('click', '.ad-list-row', function(e) {
            $(this).find('[type=radio]').attr({checked : true});
        })

        $(this.eventer).one('renderPlanList', function(e, s) {
            if (!s) {
                return false;
            }

            var ids = _this.model.get('ids');

            $.each(ids, function(k, v) {
                var planId = v.plan_id || 0;

                if (!planId) {
                    return false;
                }

                $('#plan-list-row-' + planId).find('.plan-list-row__act').trigger('click');
            })
        })

        $(this.eventer).one('renderAdList', function(e, s) {
            if (!s) {
                return false;
            }

            var ids = _this.model.get('ids');

            $.each(ids, function(k, v) {
                var planId = v.plan_id || 0,
                    adId = v.ad_id || 0;

                if (!planId) {
                    return false;
                }

                $('#ad-list-container-' + planId).find('[name=ad_id][value=' + adId + ']').attr({checked : true});
            })
        })

        this.getPlan();
    },

    getPlan : function() {
        var _this = this;

        $.getJSON('?c=Plan&a=getPlan', function(j) {
            $(_this.eventer).trigger('getPlan', [j]);
        });
    },

    getAdByPlanId : function(planId) {
        var _this = this;

        $.getJSON('?c=Ad&a=getByPlanId', {plan_id : planId}, function(j) {
            $(_this.eventer).trigger('getAdByPlanId', {plan_id : planId, d : j});
        })
    },

    renderPlanList : function(j) {
        if (!j.s) {
            $('#plan-list-msg').html(j.m).show();
            $(this.eventer).trigger('renderPlanList', [0]);

            return true;
        }

        var h = template('t-plan-list', { records : j.m});

        $('#plan-list-container').html(h);

        $(this.eventer).trigger('renderPlanList', [1]);
    },

    renderAdList : function(planId, j) {
        var domId = 'ad-list-container-' + planId,
            $planListRow = $('#plan-list-row-' + planId),
            $o = $('#' + domId);

        if (!$o[0]) {
            $o = $('<div class="ad-list-container" id="' + domId + '"></div>');
            $planListRow.after($o);
        }

        if (!j.s) {
            $o.html(j.m);

            return true;
        }

        var h = template('t-ad-list', {records : j.m});
        $o.html(h);

        $(this.eventer).trigger('renderAdList', [1]);
    }

};
